#!/bin/sh
#
# Startup script for the Audible Alarm
#
# author: Kirk Lawson <lklawson@heapy.com> 
#         Horms <horms@vergenet.net>
#
# description: sets an audible alarm running by beeping at a set interval
# processname: alarm
# config: /etc/AudibleAlarm/AudibleAlarm.conf - not yet implemented
#
#	  OCF parameters are as below:
#		OCF_RESKEY_nodelist
#		
# License: GNU General Public License (GPL)

#######################################################################
# Source function library.
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

#######################################################################
PIDFILE=${HA_VARRUN}/heartbeat-bell
#For testing
#PIDFILE=/tmp/heartbeat-bell

# What host are we running on?
us=`uname -n`

usage() {
	echo "Usage: $0 {start|stop|restart|status|monitor|meta-data|validate-all}"
	echo "  The node list is an optional space delimited"
	echo "  list of hosts that should never sound the alarm."
}

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="AudibleAlarm" version="1.0">
<version>1.0</version>

<longdesc lang="en">
Resource script for AudibleAlarm. It sets an audible alarm running by beeping 
at a set interval. 
</longdesc>
<shortdesc lang="en">Emits audible beeps at a configurable interval</shortdesc>

<parameters>
<parameter name="nodelist" unique="0">
<longdesc lang="en">
The node list that should never sound the alarm.
</longdesc>
<shortdesc lang="en">Node list</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="10s" />
<action name="stop" timeout="10s" />
<action name="restart" timeout="10s" />
<action name="status" depth="0" timeout="10s" interval="10s" />
<action name="monitor" depth="0" timeout="10s" interval="10s" />
<action name="meta-data" timeout="5s" />
<action name="validate-all" timeout="5s" />
</actions>
</resource-agent>
END
}

audiblealarm_start () {
	ocf_log info "$0: Starting"
    	if [ -f $PIDFILE ]; then
        	PID=`head -n 1 $PIDFILE`
		if [ -n "$PID" ]; then
		  ocf_log info "$0: Appears to already be running, killing [$PID]"
		  kill $PID > /dev/null
		fi
	fi
	# Use () to create a subshell to make the redirection be synchronized.
	( while [ 1 ]; do 
		sleep 1  #Sleep first, incase we bail out
		printf "\a" > /dev/console
		# Uncomment this line to cause floppy drive light
		# to flash (requires fdutils package).
		# /usr/bin/floppycontrol --pollstate > /dev/null
		#
		# To avoid issues when called by lrmd, redirect stdout->stderr.
	done &
	if echo $! >  $PIDFILE; then
		:
	else
		ocf_log info "$0: Could not write to pid file \"$PIDFILE\", bailing"
		kill $!
		return $OCF_ERR_GENERIC
	fi) >&2

	return $?
}

audiblealarm_stop () {
	ocf_log info "$0: Shutting down"
  	if [ -f $PIDFILE ]; then
		PID=`head -n 1 $PIDFILE`
		# ocf_log info "$0: Appears to already be running, killing [$PID]"
		# commented by Phost, since the confusion in the log.

		if [ -n "$PID" ]; then
		# Donnot remove PIDFILE in case the `kill` fails.
		  kill $PID > /dev/null && rm -f $PIDFILE
		fi	
	fi

	return $?
}

audiblealarm_restart () {
	audiblealarm_stop 
	audiblealarm_start 

	return $?
}

audiblealarm_status () {
  	if [ -f $PIDFILE ]; then
		PID=`head -n 1 $PIDFILE`
		if [ -n "$PID" ]; then
			echo running
			return $OCF_SUCCESS
		fi
	fi

	echo stopped
	return $OCF_NOT_RUNNING
}

audiblealarm_validate_all () {
	check_binary printf

	echo "Validate OK"
	return $OCF_SUCCESS
}
if [ $# -ne 1 ]; then
  usage
  exit $OCF_ERR_ARGS
fi

case "$1" in
   meta-data)		
	meta_data
	exit $OCF_SUCCESS
	;;
   start)
	for arg in $OCF_RESKEY_nodelist
 	  do
	    if [ "$us" = "$arg" ]; then
	      # We should not start because we are on a host
	      # listed in our argument list.
              exit $OCF_SUCCESS
	    fi
	  done
	audiblealarm_start
	;;
  stop)
	audiblealarm_stop
	;;
  restart)
	audiblealarm_restart 
	;;
  status|monitor)
	audiblealarm_status 
	;;
  validate-all)
	audiblealarm_validate_all
	;;
  usage)
	usage 
	exit $OCF_SUCCESS
	;;

  *)
	usage
	exit $OCF_ERR_ARGS
	;;
esac

exit $?
